package com.ruoyi.security.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.security.domain.AfElevator;
import com.ruoyi.security.domain.vo.AfElevatorOnlineRateBPVo;
import com.ruoyi.security.domain.vo.AfElevatorVo;
import com.ruoyi.security.domain.vo.ElevatorExcelVo;
import com.ruoyi.security.service.IAfElevatorService;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.RemoteBuildingService;
import com.ruoyi.system.api.domain.RemoteParkBuilding;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 电梯管理Controller
 *
 * @author Felix
 * @date 2021-10-12
 */
@Api(tags = " 电梯管理")
@RestController
@RequestMapping("/afElevator")
public class AfElevatorController extends BaseController {
    @Autowired
    private IAfElevatorService afElevatorService;
    @Autowired
    private RemoteBmCodeService remoteBmCodeService;

    @Autowired
    private RemoteBuildingService remoteBuildingService;


    /**
     * 查询电梯管理列表
     */
    @ApiOperation(value = "查询电梯管理列表", notes = "查询电梯管理列表")
    @PreAuthorize(hasPermi = "security:afElevator:list")
    @GetMapping("/list")
    public TableDataInfo list(AfElevator afElevator) {
        startPage();
        List<AfElevator> list = afElevatorService.selectAfElevatorList(afElevator);
        return getDataTable(list);
    }

    /**
     * 小程序查询电梯基础列表
     */
    @ApiOperation(value = "小程序查询电梯基础列表", notes = "小程序查询电梯基础列表")
    @PreAuthorize(hasPermi = "security:afElevator:list")
    @GetMapping("/appList")
    public AjaxResult baseList(AfElevatorVo vo) {
        AfElevator afElevator = new AfElevator();
        afElevator.setParkId(vo.getParkId());
        afElevator.setBuildingId(vo.getBuildingId());
        afElevator.setState(vo.getState());
        List<AfElevator> list = afElevatorService.selectAfElevatorList(afElevator);

        List<AfElevatorVo> result = new ArrayList<>();
        list.stream().forEach(o -> {
            AfElevatorVo a = new AfElevatorVo();
            BeanUtils.copyBeanProp(o, a);
            result.add(a);
        });
        return AjaxResult.success(result);
    }


    /**
     * 导出电梯管理列表
     */
    @ApiOperation(value = "导出电梯管理列表", notes = "导出电梯管理列表")
    @PreAuthorize(hasPermi = "security:afElevator:export")
    @Log(title = "电梯管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AfElevator afElevator) throws IOException {
        List<AfElevator> list = afElevatorService.selectAfElevatorList(afElevator);
        ExcelUtil<AfElevator> util = new ExcelUtil<AfElevator>(AfElevator.class);
        util.exportExcel(response, list, "电梯管理数据");
    }

    /**
     * 获取电梯管理详细信息
     */
    @ApiOperation(value = "获取电梯管理详细信息", notes = "获取电梯管理详细信息")
    @PreAuthorize(hasPermi = "security:afElevator:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(afElevatorService.selectAfElevatorById(id));
    }

    // 大屏获取电梯实时数据
    @ApiOperation(value = "获取电梯实时数据", notes = "获取电梯实时数据")
    @GetMapping(value = "/realData/{id}")
    public AjaxResult getRealData(@PathVariable("id") String id) {
        return AjaxResult.success(afElevatorService.getElevatorRealData(id));
    }

    @ApiOperation(value = "获取电梯故障列表", notes = "获取电梯故障列表")
    @GetMapping(value = "/errorList")
    public AjaxResult errorList() {
        return AjaxResult.success(afElevatorService.getElevatorErrorList());
    }


    /**
     * 新增电梯管理
     */
    @ApiOperation(value = "新增电梯管理", notes = "新增电梯管理")
    @PreAuthorize(hasPermi = "security:afElevator:add")
    @Log(title = "电梯管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AfElevator afElevator) {

        RemoteSysBmType remoteSysBmType = remoteBmCodeService.genCode(afElevator.getSysBmId(), SysBmCodeTypeValue.DEVICE);
        remoteSysBmType.getBmId();//编码类型Id
        String nowCode = remoteSysBmType.getNowCode();//生成的唯一编码
        String deviceType = remoteSysBmType.getHierarchy();//层级
        afElevator.setElevatorCode(nowCode);
        afElevator.setPropertyDesc(deviceType);
        return toAjax(afElevatorService.insertAfElevator(afElevator));
    }

    /**
     * 修改电梯管理
     */
    @ApiOperation(value = "修改电梯管理", notes = "修改电梯管理")
    @PreAuthorize(hasPermi = "security:afElevator:edit")
    @Log(title = "电梯管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AfElevator afElevator) {
        return toAjax(afElevatorService.updateAfElevator(afElevator));
    }

    @ApiOperation(value = "修改电梯配置", notes = "修改电梯配置")
    @PreAuthorize(hasPermi = "security:afElevator:edit")
    @Log(title = "电梯配置", businessType = BusinessType.UPDATE)
    @PutMapping("/config")
    public AjaxResult editConfig(@RequestBody AfElevator afElevator) {
        return toAjax(afElevatorService.updateAfElevatorConfig(afElevator));
    }


    /**
     * 删除电梯管理
     */
    @ApiOperation(value = "删除电梯管理", notes = "删除电梯管理")
    @ApiImplicitParam(name = "ids", value = "电梯管理ID数组", required = true, paramType = "path", allowMultiple = true, dataType = "String")
    @PreAuthorize(hasPermi = "security:afElevator:remove")
    @Log(title = "电梯管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(afElevatorService.deleteAfElevatorByIds(ids));
    }

    /**
     * 导入电梯记录列表,
     */
    @ApiOperation(value = "导入电梯台账列表", notes = "导入电梯台账列表")
    @PreAuthorize(hasPermi = "security:afElevator:edit")
    @Log(title = "导入电梯台账列表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ElevatorExcelVo> util = new ExcelUtil<>(ElevatorExcelVo.class);
        List<ElevatorExcelVo> elevatorExcelVos = util.importExcel(file.getInputStream());
        elevatorExcelVos.stream().forEach(o -> {
            R<RemoteParkBuilding> r = remoteBuildingService.getParkBuilding(o.getParkNum(), o.getBuildingNum());
            RemoteSysBmType bmType = remoteBmCodeService.getCodeByGenCode(o.getCode(), SysBmCodeTypeValue.DEVICE);
            if (r.getCode() == R.SUCCESS) {
                AfElevator elevator = new AfElevator();
                BeanUtils.copyProperties(o, elevator);
                RemoteParkBuilding remoteParkBuilding = r.getData();
                elevator.setBuildingId(remoteParkBuilding.getId());
                elevator.setParkId(remoteParkBuilding.getParkId());
                elevator.setElevatorCode(bmType.getNowCode());
                afElevatorService.insertAfElevator(elevator);
            }
        });
        return AjaxResult.success();
    }

    @PostMapping(value = "/listAfE")
    R<List<AfElevator>> listAfE(@RequestBody AfElevator afElevator){
        List<AfElevator> list = afElevatorService.selectAfElevatorList(afElevator);
        return R.ok(list);
    }

    //更新电梯列表
    @PostMapping(value = "/update")
    public R updateAfElevator(@RequestBody AfElevator afElevator){
        return R.ok(afElevatorService.updateAfElevator(afElevator));
    }

    //电梯设备运营
    @ApiOperation(value = "大屏电梯设备运营", notes = "大屏电梯设备运营")
    @GetMapping("/onlineRate")
    public AjaxResult onlineRate(AfElevator afElevator){
        AfElevatorOnlineRateBPVo onlineRateBPVo = afElevatorService.selectAfElevatorOnlineRate(afElevator);
        return AjaxResult.success(onlineRateBPVo);
    }

}
